import { Toast, Dialog } from 'vant'
import { removeAllSession, removeAllLocal, setLocal } from './cache'
import { removeHeaders, stopLocationService, stopMpush,saveLocalStorage,getLocalStorage,deleteLocalStorage } from '../api/android'

const allLoginOut = () => {
	let toast = Toast.loading({
		mask: true,
		duration: 0,
		message: '退出登录中...'
	})
	let userMeg = {}
	let _this = window.thisWeb
	if(window.Cordova) {
		stopLocationService()
		removeHeaders()
		stopMpush()
		getLocalStorage({key: 'userMeg'},res=>{
			userMeg = res.data.data
			if(res.success){
				deleteLocalStorage({key: 'all'},res=>{
					saveLocalStorage({key: 'userMeg',data: userMeg});
				});
			}
		});
	}
	removeAllSession()
	removeAllLocal()
	setTimeout(function() {
		Toast.clear()
		_this.$router.push({
			name: 'login'
		})
	}, 1000)
}

const outLineLoginOut = () => {
	Dialog.alert({
		message: '您的账号已在别处登录，请重新登录！'
	}).then(() => {
		allLoginOut()
	})
}

export const loginOut = (action,param) => {
	if(action == 'offline'){
		outLineLoginOut()
	}else{
		allLoginOut()
	} 
}

export const getNowRouter = () => {
	let nowRouter = null
	if(window.location.hash.includes("?") && window.location.hash.includes("/")){
		let router = window.location.hash.split("?")[0]
		let router2 = router.split("/")
		let length = (router2.length-1)*1
		nowRouter = router2[length]
	}else if(window.location.hash.includes("/")){
		let router2 = window.location.hash.split("/")
		let length = (router2.length-1)*1
		nowRouter = router2[length]
	}
	if(nowRouter) return nowRouter
}

export const goRouter = (to, fo) => {
//	let routerName = "login||tongxunlu||gonggao||index||jinjiqiuzhu||wode"
//	if(fo.fullPath == '/' && !fo.name) return
//	if(routerName.includes(to.name)) return
//	if(to.params && to.params.goBack) return
//	let nowRouter = getNowRouter()
//	let routerName2 = "cgxs_noPhotos||tsxs_noPhotos||bdzs_noPhotos||xljx_noPhotos"
//	let name = to.name
//	console.log(111,to)
//	console.log(111,fo)
//	if(nowRouter != name){
//		let param = {
//			router: fo.name,
//			query: fo.query
//		}
//		let data = JSON.stringify(param)
//		setLocal(name, data)
//	}
}
